package com.salas.admin;

import static org.jboss.seam.ScopeType.CONVERSATION;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import com.salas.entity.Attribute;
import com.salas.entity.Value;

/**
 * @author qiaoguangwei
 * 
 * 
 */
@Scope(CONVERSATION)
@Name("addAttribute")
public class AddAttributeAction {

	@Logger
	private Log log;

	@In
	FacesMessages facesMessages;
	@In
	private EntityManager entityManager;

	private String attributeName;
	@Out(required=false, scope=ScopeType.SESSION)
	private Attribute newAttribute;
	@Out(required=false, scope=ScopeType.SESSION)
	private Attribute currentAttribute;
	@Out(required=false, scope=ScopeType.SESSION)
	private Value newValue;	
    
	@Factory("newAttribute")@Begin(join=true) 
	public void start() {
		newValue = new Value();
		currentAttribute = new Attribute();
		
	}


	public void addNewAttribute() {
		newValue = new Value();
		newAttribute = new Attribute();
	}

	@End
	public void endAddNewAttribute() {				
		entityManager.persist(newAttribute);
		newValue.setAttribute(newAttribute);
		entityManager.persist(newValue);
	}

	@End
	public void mergeAttribute() {	
		newValue.setAttribute(currentAttribute);
		entityManager.persist(newValue);
		
	}

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}

	public Value getNewValue() {
		return newValue;
	}

	public void setNewValue(Value newValue) {
		this.newValue = newValue;
	}


	public Attribute getCurrentAttribute() {
		return currentAttribute;
	}


	public void setCurrentAttribute(Attribute currentAttribute) {
		this.currentAttribute = currentAttribute;
	}


}
